package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class PWDDemo {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 提示用户输入信息
        System.out.print("请输入用户名: ");
        String username = scanner.nextLine();
        System.out.print("请输入新密码: ");
        String newPassword = scanner.nextLine();
        System.out.print("请输入新昵称: ");
        String newNickname = scanner.nextLine();
        System.out.print("请输入新年龄: ");
        int newAge = scanner.nextInt();

        // 使用DBUtil获取数据库连接
        try (Connection connection = DBUtil.getConnection()) {
            // 创建SQL更新语句
            String sql = "UPDATE user SET password = ?, nickname = ?, age = ? WHERE username = ?";
            try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
                preparedStatement.setString(1, newPassword);
                preparedStatement.setString(2, newNickname);
                preparedStatement.setInt(3, newAge);
                preparedStatement.setString(4, username);

                // 执行SQL更新语句
                int num = preparedStatement.executeUpdate();
                if (num > 0) {
                    System.out.println("用户信息更新成功！");
                } else {
                    System.out.println("用户信息更新失败！");
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            scanner.close();
        }
    }
}

